﻿namespace Sidvall.Data
{
    public abstract class DataRowBase : IDataRowContextProvider
    {
        #region Public Members

        // Protected
        #region DataRowContextKey

        protected virtual string DataRowContextKey
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(this.DataRowContext.DataRowKey))
                    return this.DataRowContext.DataRowKey;
                return this.DataRowContextStorageKey;
            }
            set
            {
                this.DataRowContext.DataRowKey = value;
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected virtual string DataRowContextStorageKey
        {
            get
            {
                return null;
            }
        }

        #endregion
        #region DataRowStateIsDeleted

        protected virtual bool DataRowStateIsDeleted
        {
            get
            {
                if (this.DataRowContext.DataRowState == DataRowState.Deleted)
                    return true;
                return false;
            }
        }

        #endregion
        #region DataRowStateIsDirty

        protected virtual bool DataRowStateIsDirty
        {
            get
            {
                return this.DataRowStateIsSelfDirty;
            }
        }

        #endregion
        #region DataRowStateIsSelfDirty

        protected virtual bool DataRowStateIsSelfDirty
        {
            get
            {
                if (this.DataRowContext.DataRowState != DataRowState.Unchanged)
                    return true;
                return false;
            }
        }

        #endregion
        #region DataRowStateIsNew

        protected virtual bool DataRowStateIsNew
        {
            get
            {
                if (this.DataRowContext.DataRowState == DataRowState.Added)
                    return true;
                return false;
            }
        }

        #endregion

        #region SetDataRowContext

        public virtual void SetDataRowContext(IDataRowContextProvider item, DataRowState? dataRowState)
        {
            this.DataRowContext.Import(item, dataRowState);
        }
        public virtual void SetDataRowContext(IDataRowContextProvider item, DataRowState? dataRowState, string dataRowKey)
        {
            this.DataRowContext.Import(item, dataRowState, dataRowKey);
        }

        #endregion

        #region IDataRowContextProvider Members

        string IDataRowContextProvider.DataRowContextKey
        {
            get
            {
                return this.DataRowContextKey;
            }
            set
            {
                this.DataRowContextKey = value;
            }
        }
        string IDataRowContextProvider.DataRowContextStorageKey
        {
            get
            {
                return this.DataRowContextStorageKey;
            }
        }

        bool IDataRowContextProvider.DataRowStateIsDeleted
        {
            get
            {
                return this.DataRowStateIsDeleted;
            }
        }

        bool IDataRowContextProvider.DataRowStateIsDirty
        {
            get
            {
                return this.DataRowStateIsDirty;
            }
        }

        bool IDataRowContextProvider.DataRowStateIsSelfDirty
        {
            get
            {
                return this.DataRowStateIsSelfDirty;
            }
        }

        bool IDataRowContextProvider.DataRowStateIsNew
        {
            get
            {
                return this.DataRowStateIsNew;
            }
        }

        void IDataRowContextProvider.SetDataRowContext(IDataRowContextProvider item, DataRowState? dataRowState)
        {
            SetDataRowContext(item, dataRowState);
        }
        void IDataRowContextProvider.SetDataRowContext(IDataRowContextProvider item, DataRowState? dataRowState, string dataRowKey)
        {
            SetDataRowContext(item, dataRowState, dataRowKey);
        }

        #endregion

        #endregion
        #region Private Members

        #region DataRowContext

        private DataRowContext _DataRowContext;
        private DataRowContext DataRowContext
        {
            get
            {
                if (_DataRowContext == null)
                    _DataRowContext = new DataRowContext();
                return _DataRowContext;
            }
        }

        #endregion

        #endregion
        #region Constructors

        protected DataRowBase()
        {
        }
        protected DataRowBase(DataRowState? dataRowState)
            : this(dataRowState, null)
        {
        }
        protected DataRowBase(DataRowState? dataRowState, string dataRowKey)
        {
            if (dataRowState != null)
                this._DataRowContext = new DataRowContext(dataRowState.Value, dataRowKey);
        }

        #endregion
    }
}
